package com.gk.study.controller;

import com.gk.study.common.APIResponse;
import com.gk.study.common.ResponeCode;
import com.gk.study.entity.User;
import com.gk.study.permission.Access;
import com.gk.study.permission.AccessLevel;
import com.gk.study.service.FileService;
import com.gk.study.service.UserService;
import com.gk.study.utils.DataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService service;
    @Autowired
    FileService fileService;
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public APIResponse list(@RequestBody User user){
        List<User> list =  service.getUserList(user.getUsername());
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }


    @RequestMapping(value = "/userRegister", method = RequestMethod.POST)
    @Transactional
    public APIResponse register(User user) throws IOException {
        user.setRole("1");
        user.setStatus("0");
        service.createUser(user);
        return new APIResponse(ResponeCode.SUCCESS, "创建成功");
    }
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    @Transactional
    public APIResponse userLogin(User user) throws IOException {
        User user1=service.getUserByUserName(user.username);
        if(user1!=null&&user.getPassword().equals(user1.getPassword()))
            return new APIResponse(ResponeCode.SUCCESS, "登录成功",user1);
        else return new APIResponse(ResponeCode.FAIL, "登录失败");
    }
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @Transactional
    public APIResponse login(User user) throws IOException {
        User user1=service.getUserByUserName(user.username);
        if(user1!=null&&user.getPassword().equals(user1.getPassword())){
            if(!user1.getRole().equals("3"))
                return new APIResponse(ResponeCode.FAIL, "当前用户非管理员");
            return new APIResponse(ResponeCode.SUCCESS, "登录成功",user1);
        } else return new APIResponse(ResponeCode.FAIL, "登录失败");
    }
    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional
    public APIResponse create(User user) throws IOException {
        user.setRole("3");
        user.setStatus("0");
        service.createUser(user);
        return new APIResponse(ResponeCode.SUCCESS, "创建成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            service.deleteUser(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }

    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    @Transactional
    public APIResponse updateUserInfo(User user) throws IOException {
        if(user.getAvatarFile()!=null){
            String fileName= DataUtil.setFileName(user.getAvatarFile().getOriginalFilename());
            String path=fileService.path+"/" + fileName;
            user.getAvatarFile().transferTo(new File(path));
            user.setAvatar(fileService.urlHead+"/"+fileName);
        }
        service.updateUser(user);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @Transactional
    public APIResponse detail(String userId) throws IOException {
        User user=service.getUserDetail(userId);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功",user);
    }
    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Transactional
    public APIResponse update(User user) throws IOException {
        service.updateUser(user);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }

}
